﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace iData
{
    public class IO
    {
        public static string GetCurrentPath()
        {
            return System.Web.HttpContext.Current.Server.MapPath(@"~");
        }

          /// <summary>
        /// 写入执行日志
        /// </summary>
        public static void WriteExecuteLog(string Content)
        {
            string strDate = DateTime.Today.ToString("yyyyMMdd");
            string FileSavePath = GetCurrentPath() +"\\log" ;
            if (!Directory.Exists(FileSavePath)) Directory.CreateDirectory(FileSavePath);
            FileSavePath += "\\Log" + strDate + ".txt";
            Content = DateTime.Now.ToString() + " " + Content + "\n\r";
            FileStream fileStream = new FileStream(FileSavePath, FileMode.Append);
            StreamWriter sw = new StreamWriter(fileStream);// File.AppendText(FileSavePath);

            sw.WriteLine(Content);
            sw.Flush();
            sw.Close();
            fileStream.Close();
            sw = null;
           
        }

        /// <summary>
        /// 写入文件到指定路径
        /// </summary>
        /// <param name="Content"></param>
        /// <param name="FileSavePath"></param>
        public static void SaveFIle(string Content,string FileSavePath)
        {
            FileStream fileStream = new FileStream(FileSavePath, FileMode.Create);//FileMode.Append
            StreamWriter sw = new StreamWriter(fileStream,Encoding.UTF8);// File.AppendText(FileSavePath);
            sw.WriteLine(Content);
            sw.Flush();
            sw.Close();
            fileStream.Close();
            sw = null;

        }

        /// <summary>
        /// 写入文件到指定路径  php使用
        /// </summary>
        /// <param name="Content"></param>
        /// <param name="FileSavePath"></param>
        public static void SaveFIleNoBOM(string Content, string FileSavePath)
        {
			UTF8Encoding utf8 = new UTF8Encoding(false);
			StreamWriter  sw = new StreamWriter (FileSavePath,false,utf8);
            sw.WriteLine(Content);
            sw.Flush();
            sw.Close();
            //fileStream.Close();
            sw = null;

        }

        ////写入文件    
        //private static void WriteFile(string Content, string FileSavePath)
        //{

        //    if (System.IO.File.Exists(FileSavePath))
        //    {
        //        System.IO.File.Delete(FileSavePath);
        //    }
        //    System.IO.FileStream fs = System.IO.File.Create(FileSavePath);
        //    Byte[] bContent = System.Text.Encoding.GetEncoding("gb2312").GetBytes(Content);
        //    fs.Write(bContent, 0, bContent.Length);
        //    fs.Close();
        //    fs = null;
        //}

        ////读取文件  
        private static string ReadFile(string FilePath)
        {
            System.IO.StreamReader rd = System.IO.File.OpenText(FilePath);
            string StrRead = rd.ReadToEnd().ToString();
            rd.Close();
            return StrRead;
        }

        /// <summary>
        /// 读取模板
        /// </summary>
        /// <param name="templateName"></param>
        /// <returns></returns>
        public static string ReadTemplate(string templateName)
        {
            string FilePath = GetCurrentPath() + "\\"+ templateName;
            System.IO.StreamReader rd = System.IO.File.OpenText(FilePath);
            //string StrRead = rd.ReadToEnd().ToString();
            string StrRead = rd.ReadToEnd().ToString();
            rd.Close();
            return StrRead;
        }
       

    }
}
